﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDIApplication
{
    public partial class AllBookingscs : Form
    {
        public AllBookingscs()
        {
            InitializeComponent();
            table_load();
        }

        #region table_Load

        DataView roomDV = new DataView();
        void table_load()
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;

            //dataGridView1 Propeties
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.MultiSelect = false;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Rows[0].Selected = false;
            dataGridView1.ClearSelection();
            dataGridView1.RowTemplate.Height =20;
            dataGridView1.ScrollBars = ScrollBars.Both;

        }
        #endregion table_Load

        #region Form_Load
        private void AllBookingscs_Load(object sender, EventArgs e)
        {
            //dataGridView Display Properties
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (dataGridView1.Rows[i].Cells[3].Value.ToString() == "Available")
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                }
                else if (dataGridView1.Rows[i].Cells[3].Value.ToString() == "Reserved")
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Plum;
                }
                else if (dataGridView1.Rows[i].Cells[3].Value.ToString() == "Occupied")
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
                else if (dataGridView1.Rows[i].Cells[3].Value.ToString() == "Cleaning")
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
                }
            }
        }
        #endregion Form_Load

        #region toolStrip
        NewBooking newBookingChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            newBookingChild = new NewBooking();
            newBookingChild.MdiParent = this.MdiParent;
            newBookingChild.Location = new Point(100, 70);
            // Display the form.
            newBookingChild.Show();
            newBookingChild.Activate();
        }

        CheckIn checkInChild;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkInChild == null)
            {
                checkInChild = new CheckIn();
                checkInChild.MdiParent = this.MdiParent;
                checkInChild.Location = new Point(0, 0);
                checkInChild.Show();
                checkInChild.Activate();
            }
            this.Close();
        }

        CheckOut checkOutChild;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkOutChild == null)
            {
                checkOutChild = new CheckOut();
                checkOutChild.MdiParent = this.MdiParent;
                checkOutChild.Location = new Point(0, 0);
                checkOutChild.Show();
                checkOutChild.Activate();
            }
            this.Close();
        }

        GuestManagement guestManagementChild;
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (guestManagementChild == null)
            {
                guestManagementChild = new GuestManagement();
                guestManagementChild.Location = new Point(0, 0);
                guestManagementChild.MdiParent = this.MdiParent;
                guestManagementChild.Show();
                guestManagementChild.Activate();
            }
        }
        #endregion toolStrip

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                checkInChild = null;
                checkOutChild = null;
                newBookingChild = null;
                guestManagementChild = null;
            }
        }
        #endregion CloseAllForms

    }
}
